  function setXMLHttpRequest() {
       var xhr = null;
       // browser standard con supporto nativo
       if (window.XMLHttpRequest) {
         xhr = new XMLHttpRequest();
       }
       else if (window.ActiveXObject) {
         xhr = new ActiveXObject("Microsoft.XMLHTTP");
       }
       return xhr;
     }

     var xhrObj = setXMLHttpRequest();

	function removeCat(o,id){
		xhrObj = setXMLHttpRequest();
		var url = "modifyCat.php";
		var params = "action=remove&cat="+id;
		setCursors("wait");
		xhrObj.open("POST", url, true);

       xhrObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
       xhrObj.setRequestHeader("Content-length", params.length);
       xhrObj.setRequestHeader("Connection", "close");
       xhrObj.send(params);
       xhrObj.onreadystatechange = updatePage;
	   
	}
	
	function modifyCat(n,newN){

		
		var url = "modifyCat.php";
       var params = "action=modify&cat="+n+"&newCat="+newN;
       xhrObj.open("POST", url, true);
       setCursors("wait");

       xhrObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
       xhrObj.setRequestHeader("Content-length", params.length);
       xhrObj.setRequestHeader("Connection", "close");
       xhrObj.send(params);
       xhrObj.onreadystatechange = updatePage;
	   
		
	}	
	
	function setCursors(state){
		document.getElementById("container").style.cursor=state;
		for(i=0;i<document.getElementsByTagName("div").length;i++){
			l = document.getElementsByTagName("div").item(i);
			lClass = l.getAttribute("class");
			if(state=="wait"){
				if(lClass!=null &&(lClass=="upCat" || lClass=="downCat")){
					l.setAttribute("class",lClass+"Wait");
				}
			}
				
		}
		for(i=0;i<document.getElementsByTagName("span").length;i++){
			l = document.getElementsByTagName("span").item(i);
			lClass = l.getAttribute("class");
			if(state=="wait"){
				if(lClass!=null &&(lClass=="modCat" || lClass=="deleteCat")){
					l.setAttribute("class",lClass+"Wait");
				}
			}
				
		}
		
	}
	
	function upCat(c,id){
		if(c!='0') {
			xhrObj = setXMLHttpRequest();
			var url = "modifyCat.php";
			var params = "action=up&cat="+id+"&o="+c;
			setCursors("wait");
			
			
			
			
			xhrObj.open("POST", url, true);
			
		   xhrObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
		   xhrObj.setRequestHeader("Content-length", params.length);
		   xhrObj.setRequestHeader("Connection", "close");
		   
		   xhrObj.send(params);
		   
		   xhrObj.onreadystatechange = updatePage;
	   
		}	
	}
	
	function downCat(c,id,last){
		if(c<last) {
			xhrObj = setXMLHttpRequest();
			var url = "modifyCat.php";
			var params = "action=down&cat="+id+"&o="+c;
			setCursors("wait");
			xhrObj.open("POST", url, true);
				
		   xhrObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
		   xhrObj.setRequestHeader("Content-length", params.length);
		   xhrObj.setRequestHeader("Connection", "close");
		   xhrObj.send(params);
		   xhrObj.onreadystatechange = updatePage;		   
		}
	}
			
			
	function generaCategorie(exNovo){
		parent = document.getElementById("cat");
		if(exNovo)cats=arguments.length-1;
		if(exNovo){
			for (i=0;i<cats;i++){
				newDiv = document.createElement("div");
				newDiv.setAttribute("id","c"+i);
				//newDiv.setAttribute("name", arguments[i+1]);		
				parent.appendChild(newDiv);		
			}			
			
		}
		
		//for (child in parent.childNodes){//id=c1,c2..
		for (i=0;i<parent.childNodes.length;i++){
			child=parent.childNodes.item(i);
			
			if (exNovo){
				aChild = document.createElement("a");
				aChild.setAttribute("href","categoria.php?cat="+arguments[i+1]);//1 è exNovo
				aChild.setAttribute("name",arguments[i+1]);
				aChild.setAttribute("class","acat");
				aChild.setAttribute("id",child.id+"a");
				h3Child = document.createElement("h3");
				h3Child.setAttribute("class","categoria");
				h3Child.setAttribute("id",arguments[i+1]);
				aText = document.createTextNode(arguments[i+1]);
				aChild.appendChild(aText);
				h3Child.appendChild(aChild);	
				//child.appendChild(h3Child);							
			}else{
				h3Child=document.getElementById(arguments[i+1]);
				child.removeChild(h3Child);
			}
			
			
			divX = document.createElement("div");
			divX.setAttribute("id",child.id+"x");
			
			
			spanX = document.createElement("span");
			theRealName="'"+arguments[i+1]+"'";
			theRealPosition = i;
			spanX.setAttribute("onclick","if(confirm(\"Rimuovo \"+"+theRealName+"+\" ?\")) removeCat("+theRealPosition+", "+theRealName+")");
			spanX.setAttribute("class", "deleteCat");
			spanX.setAttribute("title","Cancella");
			
			textX = document.createTextNode('x');
						
			spanM = document.createElement("span");
			spanM.setAttribute("id",child.id+"m");
			spanM.setAttribute("class","modCat");
			spanM.setAttribute("title","Cambia il Titolo");
			spanM.setAttribute("onclick","n=prompt(\"Nuovo Nome?\"); if(n!=null) modifyCat("+theRealName+", n);");
			imgM = document.createElement("img");
			imgM.setAttribute("src","images/modifica.gif");
			imgM.setAttribute("class","imgM");
			spanM.appendChild(imgM);
			divX.appendChild(spanM);
			divX.appendChild(spanX);
			spanX.appendChild(textX);
			
			
			divD = document.createElement("div");
			divD.setAttribute("id",child.id+"d");
			divD.setAttribute("class", "downCat");
			divD.setAttribute("title","Sposta Giù");
			divD.setAttribute("onclick","downCat("+theRealPosition+", "+theRealName+", "+(parent.childNodes.length-1)+")");

			textD = document.createTextNode("↓");
			divD.appendChild(textD);
			divU = document.createElement("div");
			divU.setAttribute("id",child.id+"u");
			divU.setAttribute("class", "upCat");
			divU.setAttribute("title","Sposta Su");
			divU.setAttribute("onclick","upCat("+theRealPosition+", "+theRealName+")");
			textU = document.createTextNode("↑");
			divU.appendChild(textU);	
			divM = document.createElement("div");
			divM.setAttribute("class", "modifyCat");
			divM.appendChild(divU);
			divM.appendChild(divX);
			divM.appendChild(divD);
			child.appendChild(divM);
			child.appendChild(h3Child);							
			
			
		}
	
	}		
	
	
	

     function updatePage() {	
		
		 if (xhrObj.readyState==4) {
			risp = xhrObj.responseText;
			links = risp.split(",");
			document.getElementById("cat").innerHTML="";			
			generaCategorie.apply(null,links);			
			setCursors("default");
		}
	
	
     }

     function controllaCerca() {
	if(document.forms[1].elements[1].value == "") {
		alert('Devi inserire almeno un termine per la ricerca!', '');
		return false;
	}
	pattern = /[0-9]{1,2}[/]{1}[0-9]{1,2}[/]{1}[0-9]{4}/;
	if((document.getElementById("minimo").value == "") || document.getElementById("minimo").value.search(pattern) != -1)
		if((document.getElementById("massimo").value == "") || document.getElementById("massimo").value.search(pattern) != -1)
			return true;
	alert('Data inserita in modo non corretto. Formato corretto: dd/mm/aaaa','');
	return false;
     }

    function controllaRegistra() {
	if(document.forms[1].elements[1].value == "") {
		alert('Devi inserire un nome utente!', '');
		return false;
	}
	if(document.forms[1].elements[2].value == "") {
		alert('Devi inserire una password!', '');
		return false;
	}
	if(document.forms[1].elements[2].value != document.forms[1].elements[3].value) {
		alert('Le password inserite non corrispondono!', '');
		return false;
	}
	pattern = /[@]{1}/
	if((document.forms[1].elements[11].value == "") || (document.forms[1].elements[11].value.search(pattern) == -1)) {
		alert('Devi inserire un indirizzo e-mail valido!', '');
		return false;	
	}
	pattern = /[0-9]{1,2}[/]{1}[0-9]{1,2}[/]{1}[0-9]{4}/;
	if((document.forms[1].elements[8].value != "") && ((document.forms[1].elements[8].value.search(pattern) == -1) || (document.forms[1].elements[8].value.length > 10))) {
		alert('Il valore immesso per la data di nascita non è valido. Inserirlo nel formato dd/mm/aaaa. Esempio: 02/05/1988', '');
		return false;
	}
	return true;
     }
